<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<link href="skin/style.css" rel="stylesheet" media="screen" type="text/css" title="style" />
	<link href="skin/style_compte.css" rel="stylesheet" media="screen" type="text/css" title="style" />
	<link href="skin/style_formulaire_inscription.css" rel="stylesheet" media="screen" type="text/css" title="style" />
	<script type="text/javascript" src="js/iframe.js"></script>
	<script type="text/javascript" src="js/ajax.js"></script>
	<script type="text/javascript" src="js/cadre_compte.js"></script>
	<script type="text/javascript" src="js/formulaire_inscription.js"></script>
</head>
<body onload="SetTitle('Mon compte');" style="margin: 0;">
		
<?php

	$rootdir = './';
	
	include_once('functions/session.php');
	include_once('functions/orders.php');
	include_once('functions/pays.php');
	include_once('functions/users.php');
	include_once('functions/status.php');
	
	if(isset($_GET['action']) && $_GET['action'] == 'disconnect')
		unset($_SESSION['user']);
	
	include('menu_left.php');
		
?>
	<div id="compte-contenu">
		<div id="compte-header"></div>
		<div id="compte-titre-initial"></div>	
	<?php
	
		if(isset($_POST['send']))
		{
			if(isset($_SESSION['user']))
				$user = $_SESSION['user'];
			else
				echo '<p style="margin-top: 20px; margin-left: 20px; color: red;">Erreur</p>';
		
			$updateduser = array();
			$updateduser['nom'] = $_POST['nom'];
			$updateduser['prenom'] = $_POST['prenom'];
			$updateduser['mail'] = $_POST['mail'];
			$updateduser['adresse_voie1'] = $_POST['voie1'];
			$updateduser['adresse_voie2'] = $_POST['voie2'];
			$updateduser['adresse_codepostal'] = $_POST['codepostal'];
			$updateduser['adresse_ville'] = $_POST['ville'];
			$updateduser['adresse_pays'] = $_POST['pays'];
			$updateduser['tel'] = $_POST['tel'];
			$updateduser['password'] = $user['password'];
			$updateduser['admin'] = $user['admin'];
			$updateduser['idscommands'] = $user['idscommands'];
			$updateduser['valid'] = $user['valid'];
			$updateduser['id'] = $user['id'];
			
			if(UpdateUser($updateduser))
			{
				$_SESSION['user'] = $updateduser;
				echo '<p style="margin-top: 20px; margin-left: 20px; color: green; font-size: 70%;">Compte mis à jour.</p>';
			}
			else
				echo '<p style="margin-top: 20px; margin-left: 20px; color: red;">Erreur lors de la mise à jour du compte.</p>';
		}
	
		if(isset($_SESSION['user']['mail']))
			echo GetHTMLIfConnected();
		else
			echo '<p style="margin-top: 20px; margin-left: 20px;">Vous n\'êtes pas connecté.</p>';
	?>
	</div>

	<hr style="clear: both; visibility: hidden;" />
	
</body>
</html>

<?php

function GetHTMLIfConnected()
{	
	if(isset($_SESSION['user']))
		$user = $_SESSION['user'];
	else
		return 'Erreur';
	
	if(isset($_GET['show']))
	{
		if($_GET['show'] == 'orders')
		{
			$showorders = 'block';
			$showinfos = 'none';
		}
		else if($_GET['show'] == 'infos')
		{
			$showorders = 'none';
			$showinfos = 'block';
		}
		else
		{
			$showorders = 'none';
			$showinfos = 'none';
		}
	}
	else
	{
		$showorders = 'none';
		$showinfos = 'none';
	}

	$HTML = 
	'
	
	<div id="compte_initial_suivrecommande">
		<div id="compte_initial_suivrecommande_carrerouge">
		</div>
		<div style="margin-left: 25px; margin-top: 0px;">
			<a href="" onclick="ShowOrders(); return false;">Suivre mes commandes</a>
		</div>
		<div id="compte_divorders" style="display: ' . $showorders .';">				
			<table style="font-size: 80%; width: 100%;">
				<tr>
					<th style="border: 1px solid gray;">Num de commande</th>
					<th style="border: 1px solid gray;">Date de commande</th>
					<th style="border: 1px solid gray;">Statut</th>
				</tr>
';
					

	$orders = explode(' ', $user['idscommands']);
	
	if($user['idscommands'] != '')
	{
		foreach($orders as $orderID)
		{
			$order = GetOrderByID($orderID);
			if($order)
			{
				$Statut = GetStatutById($order['status']);
			
				$HTML .= '
				<tr>
					<td style="border: 1px solid gray;">
					' . $order['id'] . '
					</td style="border: 1px solid gray;">
					<td style="border: 1px solid gray;">
					' . $order['date'] . '
					</td>
					<td style="border: 1px solid gray;">
					' . $Statut . '
					</td>
				</tr>';
			}
		}
	}

	$HTML .= '
			</table>				
		</div>
	</div>
	<div id="compte_initial_modifierinfos">
		<div id="compte_initial_modifierinfos_carrebleu">
		</div>
		<div style="margin-left: 25px; margin-top: 0px;">
			<a href="" onclick="ShowInfos(); return false;">Consulter ou modifier mes informations personnelles</a>
			<div id="compte_divinfos" style="display: ' . $showinfos .';">
			<form method="post" action="?show=infos" onsubmit="return CheckAllCadreCompte()?true:false;" style="font-size: 80%;">
				<table>
					<tr>
						<td><label for="nom">Nom*</label></td>
						<td><input value="' . $user['nom'] . '" type="text" tabindex="1" name="nom" id="nom" size="30" onkeyup="CheckNom();" onblur="CheckNom();"/><br /></td>
						<td><div id="nom-icone"></div><div id="nom-txt" class="txterreur"></div></td>
					</tr>
					<tr>
						<td><label for="prenom">Prénom*</label></td>
						<td><input value="' . $user['prenom'] . '" type="text" tabindex="1" name="prenom" id="prenom" size="30" onkeyup="CheckPrenom();" onblur="CheckPrenom();"/><br /></td>
						<td><div id="prenom-icone"></div><div id="prenom-txt" class="txterreur"></div></td>
					</tr>
					<tr>
						<td><label for="mail">Adresse Mail*</label></td>
						<td><input value="' . $user['mail'] . '" type="text" tabindex="1" name="mail" id="mail1" size="30" onkeyup="CheckMail1(); CheckMail2();" onblur="CheckExistingEmail(\'' . $user['mail'] . '\');"/><br /></td>
						<td><div id="mail1-icone"></div><div id="mail1-txt" class="txterreur"></div></td>
					</tr>
					<tr>
						<td><label for="mail2">Confirmer adresse Mail*</label></td>
						<td><input value="' . $user['mail'] . '" type="text" tabindex="1" name="mail2" id="mail2" size="30" onkeyup="CheckMail2();" onblur="CheckMail2();"/><br /></td>
						<td><div id="mail2-icone"></div><div id="mail2-txt" class="txterreur"></div></td>
					</tr>
					<tr>
						<td><br /></td>
					</tr>
					<tr>
						<td><br /></td>
					</tr>
					<tr>
						<td><label for="voie1">Adresse*</label></td>
						<td><input value="' . $user['adresse_voie1'] . '" type="text" tabindex="2" name="voie1" id="voie1" size="30" onkeyup="CheckVoie();" onblur="CheckVoie();"/><br /></td>
						<td><div id="voie-icone"></div><div id="voie-txt" class="txterreur"></div></td>
					</tr>
					<tr>
						<td><label for="voie2"></label></td>
						<td><input value="' . $user['adresse_voie2'] . '" type="text" tabindex="2" name="voie2" id="voie2" size="30" onkeyup="CheckVoie();" onblur="CheckVoie();"/><br /></td>
					</tr>
					<tr>
						<td><label for="codepostal">Code postal*</label></td>
						<td><input value="' . $user['adresse_codepostal'] . '" type="text" tabindex="2" name="codepostal" id="codepostal" size="30" onkeyup="CheckCodePostal();" onblur="CheckCodePostal();"/><br /></td>
						<td><div id="codepostal-icone"></div><div id="codepostal-txt" class="txterreur"></div></td>
					</tr>
					<tr>
						<td><label for="ville">Ville*</label></td>
						<td><input value="' . $user['adresse_ville'] . '" type="text" tabindex="2" name="ville" id="ville" size="30" onkeyup="CheckVille();" onblur="CheckVille();"/><br /></td>
						<td><div id="ville-icone"></div><div id="ville-txt" class="txterreur"></div></td>
					</tr>
					<tr>
						<td><label for="pays">Pays</label></td>
						<td><select name="pays" id="pays">';

	$countries = GetAllCountries();
	foreach($countries as $country)
	{
		if($country['nom'] == $user['adresse_pays'])
			$add = 'selected ';
		else
			$add = '';
			
		$HTML .= '
								<option ' . $add . 'value="' . $country['nom'] . '">' . $country['nom'] . '</option>';
	}
	
	$HTML .= '
							</select><br />
						</td>
					</tr>
					<tr>
						<td><br /></td>
					</tr>
					<tr>
						<td><label for="tel">Numéro de téléphone</label></td>
						<td><input value="' . $user['tel'] . '" type="text" tabindex="2" name="tel" id="tel" size="30" onkeyup="CheckTel();" onblur="CheckTel();"/><br /></td>
					</tr>
					<tr>
						<td><br /><td>
					</tr>
					<tr>
						<td style="text-align: left;"><input type="submit" name="send" tabindex="12" value="Mettre à jour le compte"/></td>
						<td><a href="?show=infos">Annuler les modifications</a></td>
					</tr>
				</table>
			</form>
			</div>
		</div>
	</div>
	<div id="compte_deconnexion"><a onclick="parent.Connected = false;" target="iframe" href="cadre_compte.php?action=disconnect"></a><br /></div>
	<br />
';
	
	return $HTML;
}

?>
